# OpenMV端代码（main.py）
# 功能：水面垃圾识别并通过UART发送坐标
import sensor, image, time, pyb

# 初始化摄像头
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)

# 定义水面垃圾颜色阈值（需要根据实际环境调整）
GARBAGE_THRESHOLDS = [
    (30, 70, -20, 20, -30, 30)  # 灰色漂浮物阈值
]

# 初始化UART
uart = pyb.UART(3, 115200)

def detect_garbage(img):
    blobs = img.find_blobs(GARBAGE_THRESHOLDS, area_threshold=100, merge=True)
    if blobs:
        largest = max(blobs, key=lambda b: b.area())
        img.draw_rectangle(largest.rect(), color=(0, 255, 0))
        img.draw_cross(largest.cx(), largest.cy(), color=(0, 255, 0))
        return (largest.cx(), largest.cy(), largest.area())
    return None

while True:
    img = sensor.snapshot()
    result = detect_garbage(img)
    if result:
        uart.write("DETECT %d %d %d\n" % result)  # 发送检测结果
    else:
        uart.write("CLEAR\n")
    time.sleep_ms(100)














